package com.github.andy0106.kersstcli.client;

import com.github.andy0106.kersstjtl.api.ApiBuffer;
import com.github.andy0106.kersstjtl.api.Package;
import com.github.andy0106.kersstjtl.files.File;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;

public class KersstController {
    public TextField minecraft_dir_input;
    public TextField ip_input;
    public TextField port_input;
    public Button lock_config_button;
    public Button unlock_config_button;
    public ListView<String> all_package_listview;
    public ListView<String> installed_package_listview;
    public Label package_name_display;
    public Label package_version_display;
    public Label description_display;
    public ListView<String> mods_listview;
    public Button install_button;
    public Button uninstall_button;
    public Button update_button;

    private ArrayList<Package> selected_packages = new ArrayList<>();

    public void init() {
        unlock_config_button.setDisable(true);
        ip_input.setText("kersst.api");
        port_input.setText("8000");
        minecraft_dir_input.setText(".minecraft");
        all_package_listview.focusedProperty().addListener((observableValue, old_value, value) -> {
            install_button.setDisable(false);
            uninstall_button.setDisable(true);
            update_button.setDisable(true);
        });
        all_package_listview.getSelectionModel().selectedItemProperty().addListener((observableValue, old_value, value) -> {
            if (value != null) {
                selected_packages.clear();
                selected_packages.addAll(ApiBuffer.getPackagesByName(value));
                selected_packages.get(0).loadPackage();
                package_name_display.setText(selected_packages.get(0).getName());
                package_version_display.setText(selected_packages.get(0).getPack_version() + " for " + selected_packages.get(0).getGame_version());
            }
        });
        installed_package_listview.focusedProperty().addListener((observableValue, old_value, value) -> {
            install_button.setDisable(true);
            uninstall_button.setDisable(false);
            update_button.setDisable(false);
        });
        installed_package_listview.getSelectionModel().selectedItemProperty().addListener((observableValue, old_value, value) -> {
            if (value != null) {
                selected_packages.clear();
                selected_packages.add(Local.getPackageByName(value));
                package_name_display.setText(selected_packages.get(0).getName());
                package_version_display.setText(selected_packages.get(0).getPack_version() + " for " + selected_packages.get(0).getGame_version());
            }
        });
        install_button.setDisable(true);
        uninstall_button.setDisable(true);
        update_button.setDisable(true);
    }

    public static ObservableList<String> convert(Collection<String> packages) {
        ObservableList<String> items = FXCollections.observableArrayList();
        items.addAll(packages);
        return items;
    }

    public static ObservableList<String> convert2(ArrayList<Package> packages) {
        ObservableList<String> items = FXCollections.observableArrayList();
        for (Package pack : packages) {
            items.add(pack.getName());
        }
        return items;
    }

    public void onLockButtonClick() {
        minecraft_dir_input.setEditable(false);
        ip_input.setEditable(false);
        port_input.setEditable(false);
        lock_config_button.setDisable(true);
        unlock_config_button.setDisable(false);
        all_package_listview.setDisable(false);
        installed_package_listview.setDisable(false);

        ApiBuffer.setApiAddress(ip_input.getText(), port_input.getText());
        ApiBuffer.CACHE_DIR = minecraft_dir_input.getText();
        ApiBuffer.load();
        ApiBuffer.get();
        Local.setMCDIR(minecraft_dir_input.getText());
        all_package_listview.setItems(convert(ApiBuffer.getAllPackagesName()));
        installed_package_listview.setItems(convert(Local.getAllPackagesName()));

        new File("packagescache.json", minecraft_dir_input.getText()).create();
    }

    public void onUnlockButtonClick() {
        minecraft_dir_input.setEditable(true);
        ip_input.setEditable(true);
        port_input.setEditable(true);
        lock_config_button.setDisable(false);
        unlock_config_button.setDisable(true);
        all_package_listview.setDisable(true);
        installed_package_listview.setDisable(true);
        install_button.setDisable(true);
        uninstall_button.setDisable(true);
        update_button.setDisable(true);
    }

    public void onInstallButtonClick() {
        ApiBuffer.getLatestPackageByName(package_name_display.getText()).install_without_load(minecraft_dir_input.getText() + "/versions/");
    }

    public void onUninstallButtonClick() {
        try {
            Files.deleteIfExists(Paths.get(Local.getMCDIR(), selected_packages.get(0).getName()));
        } catch (IOException e) {
            System.out.println(Paths.get(Local.getMCDIR(), selected_packages.get(0).getName()).toString() + " delete failed");
            return;
        }
        System.out.println(Paths.get(Local.getMCDIR(), selected_packages.get(0).getName()).toString() + " delete successfully");
    }

    public void onUpdateButtonClick() {

    }
}